/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Historic;

/**
 *
 * @author Adriano
 */
public class HistoricDAO implements DAO {

    private final DataSource dataSource;

    public HistoricDAO() {
        dataSource = new DataSource();
    }

    public void create(Object o) {

        try {

            String sql = "INSERT INTO historic VALUES (null,?,?,date_format(curdate(),'%d%m%Y'))";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            Historic historico = (Historic) o;

            ps.setString(1, historico.getTxtDescricao());
            ps.setString(2, historico.getLstStatus());

            int res = ps.executeUpdate();
            if (res == 0) {
                System.err.println("HISTORICODAO.CREATE - Nenhum historico criado !");
            }
        } catch (SQLException e) {
            System.err.println("HISTORICODAO.CREATE  - " + e.getMessage());
        }

    }

    public void delete(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(Object o) {
        try {

            String sql = "UPDATE historic SET descricao=? , status=? where id=?";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            Historic query = (Historic) o;

            ps.setString(1, query.getTxtDescricao());
            ps.setString(2, query.getLstStatus());
            ps.setInt(3, query.getTxtId());
            ps.executeUpdate();

        } catch (SQLException e) {
            System.err.println("UPDATEHISTORICO- " + e.getMessage());
        }
    }

    public Object findDesc(String key) {
        try {
            String sql = "SELECT * FROM  historic WHERE descricao like ?";

            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            ps.setString(1, "%" + key +"%");
            ArrayList<Historic> listaHistoricoDescricao = new ArrayList<Historic>();
                      
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                Historic historic = new Historic();
                historic.setTxtDescricao(rs.getString("Historic.descricao"));
                //cont.setTxtId(rs.getInt("Contador.id"));

                listaHistoricoDescricao.add(historic);

            }
            return listaHistoricoDescricao;

        } catch (SQLException e) {
            System.err.println("HISTORICO2.READ - " + e.getMessage());
            return null;
        }
    }

    public Object findAll() {
        try {
            String sql = "SELECT * FROM historic";

            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            ArrayList<Historic> listaHistorico = new ArrayList<Historic>();

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                Historic historic = new Historic();

                historic.setTxtDescricao(rs.getString("Historic.descricao"));
                //cont.setTxtId(rs.getInt("Contador.id"));

                listaHistorico.add(historic);

            }
            return listaHistorico;

        } catch (Exception e) {
            System.err.println("HISTORICO.READ - " + e.getMessage());
            return null;
        }
    }

    public Object find(Object o) {

        try {

            String sql = "SELECT * FROM historic WHERE descricao = ?";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            Historic query = (Historic) o;

            ps.setString(1, query.getTxtDescricao());

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {

                Historic historico = new Historic();

                historico.setTxtDescricao(rs.getString("Descricao"));
                historico.setLstStatus(rs.getString("Status"));
                historico.setTxtId(rs.getInt("Id"));

                return historico;
            }
        } catch (SQLException e) {
            System.err.println("BUSCAHISTORICO.READ - " + e.getMessage());
        }
        return null;

    }

//       public Object findDesc(String descricao) {
//          try {  
//            String sql = "SELECT * FROM historico where descricao like %?%";
//             
//            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
//
//            ArrayList<Historico> listaHistorico = new ArrayList<Historico>();
//
//            ResultSet rs = ps.executeQuery();
//
//            while (rs.next()) {
//
//                Historic historic = new Historic();
//
//                historic.setTxtDescricao(rs.getString("Historic.descricao"));
//                //cont.setTxtId(rs.getInt("Contador.id"));
//             
//                listaHistorico.add(historic);
//
//            }
//            return listaHistorico;
//
//        } catch (Exception e) {
//            System.err.println("HISTORICO.READ - " + e.getMessage());
//            return null;
//        }
//    }
//    
    public void closeDAO() {
        dataSource.closeConnection();
    }

    public Object read(Object o) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
